package ink.yuanchu.picachoframework.controller;

import ink.yuanchu.picachoframework.config.FileConfig;
import ink.yuanchu.picachoframework.dao.UserMapper;
import ink.yuanchu.picachoframework.entity.User;
import ink.yuanchu.picachoframework.exception.BusinessException;
import io.swagger.annotations.Api;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created with IntelliJ IDEA.
 *
 * @author ziyuan
 * @Date: 2021/02/19/13:31
 * @Description:
 */
@RestController
@RequestMapping("/picachoframework/test")
@Api("全局异常测试类")
public class TestApi {

    @Autowired
    private UserMapper userMapper;

    @Autowired
    private FileConfig fileConfig;

    @PostMapping("/businessException")
    public boolean businessException(){
        User user = null;
        if (user == null){
            throw new BusinessException("-1","用户为空");
        }
        return true;
    }

    @PostMapping("/nullPointerException")
    public boolean nullPointerException(){
        //这里故意造成一个空指针的异常，并且不进行处理
        String str=null;
        str.equals("111");
        return true;
    }

    @DeleteMapping("/otherException")
    public boolean otherException(){
        Integer.parseInt("abc123");
        return true;
    }

    @GetMapping("/selectAll")
    public List<User> selectAll(){
        return userMapper.selectAll();
    }

    @GetMapping("/getMapData")
    public List<Map> getMapData() {
        List<Map> list = new ArrayList();
        for (int i = 0; i < 5; i++) {
            Map map = new HashMap();
            map.put("item","item"+i);
            map.put("count",20);
            list.add(map);
        }
        return list;
    }

    @GetMapping("/getFilePath")
    public String getFilePath(){
        return fileConfig.getUploadPath();
    }
}
